package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.MemberMapper;
import com.liuzu.esys.mapper.PrerogativeMapper;
import com.liuzu.esys.pojo.Member;
import com.liuzu.esys.service.MemberService;
import com.liuzu.esys.vo.YyhBarVo;
import com.liuzu.esys.vo.YyhPieVo;
import org.apache.ibatis.jdbc.RuntimeSqlException;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 会员 Service层 实现类
 * 会员服务层的方法
 */
@Service("MemberService")
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private PrerogativeMapper prerogativeMapper;
    //分页查询功能
    @Override
    public LayuiPageVo<Member> pagingquery(Map<String, Object> params) {
        //mybatis 的 分页查询
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page,limit);

        List<Member> Members = memberMapper.MemberList(params);
        
        //mybatis 的 分页数据结构
        PageInfo<Member> studentPageInfo = new PageInfo<>(Members);
        //把 mybatis 的分页数据封装到 layui的分页数据结构中

        LayuiPageVo<Member> studentLayuiPageVo = new LayuiPageVo<>();
        studentLayuiPageVo.setCode(0);  // layui 默认 0 是正确的状态码
        studentLayuiPageVo.setMsg("分页列表数据"); // 提示信息
        studentLayuiPageVo.setCount(studentPageInfo.getTotal()); // 总记录数
        studentLayuiPageVo.setData(studentPageInfo.getList());  // 分页的列表数据
        return studentLayuiPageVo;
    }
    //删除功能
    @Override
    @Transactional(rollbackFor = RuntimeSqlException.class,propagation = Propagation.REQUIRED)
    public boolean memberdel(Integer id) {
        //根据id 查询名字

         String vip_name = memberMapper.selectmemberdel(id);

        boolean flag = false;
        int a1 = memberMapper.memberdel(id);
        int a2 = memberMapper.Prerogative(vip_name);
            if(a1 == 1 && a2 == 1){
                flag = true;
            }else {
                throw new RuntimeSqlException();
            }
         return flag;
    }

    /**
     * 添加操作
     * 同时添加 会员表 和  会员特权表
     */
    @Override
    @Transactional(rollbackFor = RuntimeSqlException.class,propagation = Propagation.REQUIRED)
    public boolean memberAdd(Member member) {
        //测试 是否可以接收到前面数据
       // System.out.println(member);
        boolean flag = false;
        switch (member.getVip_age()){
            case 1:
                member.setVip_privilege("青铜级");
                member.setVip_grade(1);
                break;
            case 2:
            case 3:
                member.setVip_privilege("白银级");
                member.setVip_grade(2);
                break;
            case 4:
            case 5:
                member.setVip_privilege("黄金级");
                member.setVip_grade(3);
                break;
            default:
                member.setVip_privilege("尊贵级上座");
                member.setVip_grade(4);
            break;
        }
       int a1 =  memberMapper.MemberAdd(member);
       int a2 =  prerogativeMapper.prerogativeAdd(member);
       if(a1 == 1 && a2 == 1) {
           flag = true;
       }else {
           throw new RuntimeSqlException();
       }

        return flag;
    }

    @Override
    @Transactional(rollbackFor = RuntimeSqlException.class,propagation = Propagation.REQUIRED)
    public boolean memberSet(Member member) {
        boolean flag = false;
        switch (member.getVip_age()){
            case 1:
                member.setVip_privilege("青铜级");
                member.setVip_grade(1);
                break;
            case 2:
            case 3:
                member.setVip_privilege("白银级");
                member.setVip_grade(2);
                break;
            case 4:
            case 5:
                member.setVip_privilege("黄金级");
                member.setVip_grade(3);
                break;
            default:
                member.setVip_privilege("尊贵级上座");
                member.setVip_grade(4);
                break;
        }
        int a1 = memberMapper.MemberSet(member);
        int a2 = prerogativeMapper.prerogativeSet(member);
        if(a1 == 1 && a2 == 1){
            flag = true;
        }else {
            throw new RuntimeSqlException();
        }

        return flag;
    }
    //会员等级比例图
    @Override
    public List<YyhPieVo> memberBar() {
       List<YyhPieVo> list =  prerogativeMapper.prerogativeBar();
        return list;
    }

}
